package cn.zjweu.servlet;

import cn.zjweu.domain.Dish;
import cn.zjweu.domain.MessageInfo;
import cn.zjweu.domain.ResultInfo;
import cn.zjweu.domain.UserCart;
import cn.zjweu.service.CartService;
import cn.zjweu.service.impl.CartServiceImpl;
import cn.zjweu.utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/cart/*")
public class CartServlet extends BaseServlet {

    private CartService cartService = new CartServiceImpl();

    /*修改、添加购物车*/
    public void editCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int userId = 0;
        int dishId = 0;
        int count = 0;
        int flag = 0;
        UserCart userCart = null;
        ResultInfo info = new ResultInfo();
        try {
            flag = Integer.parseInt(request.getParameter("flag"));
        }catch (Exception e){
            flag = 0;
        }
        try {
            userId = Integer.parseInt(request.getParameter("userId"));
            dishId = Integer.parseInt(request.getParameter("dishId"));
            count = Integer.parseInt(request.getParameter("count"));
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("数据错误！");
            writeValue(info, response);
            return;
        }

        userCart = cartService.findCartByUserAndDish(userId, dishId);
        //如果菜品在购物车
        if (userCart != null) {
            System.out.println(userCart + "\n菜品存在购物车-修改");

            if (flag > 1) userCart.setCount(count);
            else userCart.setCount(count + userCart.getCount());

            //更新购物车
            if (cartService.editCountInCart(userCart)) {
                System.out.println("更新购物车菜品成功：" + userCart);

                info.setFlag(MessageInfo.SUCCESS);
                info.setData(userCart);
                info.setInfoMsg("更新购物车菜品成功！");
            } else {
                System.out.println("更新购物车菜品失败：" + userCart);

                info.setFlag(MessageInfo.FAIL);
                info.setData(userCart);
                info.setInfoMsg("更新购物车菜品失败！");
            }
            writeValue(info, response);
        } else {
            System.out.println("菜品不在购物车-添加");

            userCart = new UserCart(userId, dishId, count);
            //添加购物车
            if (cartService.saveToCart(userCart)) {
                System.out.println("添加购物车成功：" + userCart);

                info.setFlag(MessageInfo.SUCCESS);
                info.setData(userCart);
                info.setInfoMsg("添加购物车成功！");
            } else {
                System.out.println("添加购物车失败：" + userCart);

                info.setFlag(MessageInfo.FAIL);
                info.setData(userCart);
                info.setInfoMsg("添加购物车失败！");
            }
            writeValue(info, response);
        }
    }

    /*删除购物车*/
    public void delCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int userId = 0;
        int dishId = 0;
        try {
            userId = Integer.parseInt(request.getParameter("userId"));
            dishId = Integer.parseInt(request.getParameter("dishId"));
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("数据错误！");
            writeValue(info, response);
            return;
        }

        if (cartService.clearCartByUserIdOrDishId(userId, dishId)) {
            System.out.println("删除菜品成功");

            info.setFlag(MessageInfo.SUCCESS);
            info.setData(userId);
            info.setInfoMsg("成功删除菜品！");
            writeValue(info, response);
        } else {
            System.out.println("删除菜品失败");

            info.setFlag(MessageInfo.FAIL);
            info.setData(userId);
            info.setInfoMsg("删除失败！");
            writeValue(info, response);
        }
    }

    /*清空购物车*/
    public void clearCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int userId = 0;
        try {
            userId = Integer.parseInt(request.getParameter("userId"));
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("数据错误！");
            writeValue(info, response);
            return;
        }

        if (cartService.clearCartByUserId(userId)) {
            System.out.println("清空购物车成功");

            info.setFlag(MessageInfo.SUCCESS);
            info.setData(userId);
            info.setInfoMsg("成功清空购物车！");
            writeValue(info, response);
        } else {
            System.out.println("清空购物车失败");

            info.setFlag(MessageInfo.FAIL);
            info.setData(userId);
            info.setInfoMsg("清空失败！");
            writeValue(info, response);
        }
    }

    /*查看用户购物车*/
    public void cartList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int userId = 0;
        try {
            userId = Integer.parseInt(request.getParameter("userId"));
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("数据错误！");
            writeValue(info, response);
            return;
        }

        List<Dish> list = cartService.findCartListByUserId(userId);
        if (list == null) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("未找到购物车信息！");
        } else {
            info.setFlag(MessageInfo.SUCCESS);
            info.setData(list);
            info.setInfoMsg("找到ID为" + userId + "用户的购物车信息！");
        }
        writeValue(info, response);


    }

}
